#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 14:43
from domain.report.common.period_enum import PeriodEnum
from domain.report.common.str_util import del_html_element
from domain.report.dto.balance_sheet_dto import BalanceSheetDTO, BalanceSheetSchema
from typing import List

_value_index_name = ["money_cap", "sett_rsrv", "loanto_oth_bank_fi", "tradable_fnncl_assets", "bills_receivable",
                     "account_receivable", "pre_payment", "premium_receiv", "reinsur_receiv", "reinsur_res_receiv",
                     "interest_receivable", "dividend_receivable", "other_receivable", "pur_resale_fa", "inventories",
                     "nca_within_1y", "other_current_assets", "total_current_assets", "decr_in_disbur",
                     "saleable_finacial_asset", "held_to_maturity_invest", "lt_receivable", "lt_equity_invest",
                     "invest_property_estate", "fixed_asset", "construction_in_process", "project_goods_and_material",
                     "fixed_assets_disposal", "productive_biological_asset", "oil_and_gas_assets", "intangible_assets",
                     "dev_expenditure", "goodwill", "lt_to_expense", "total_dta", "other_noncurrent_assets",
                     "total_noncurrent_assets", "total_assets", "st_loan", "cb_borr", "depos_ib_deposits",
                     "loan_oth_bank", "tradable_fnncl_liab", "bill_payable", "accounts_payable", "pre_receivable",
                     "sold_for_repur_fa", "comm_payable", "payroll_payable", "taxes_payable", "interest_payable",
                     "dividend_payable", "other_payable", "payable_to_reinsurer", "rsrv_insur_cont",
                     "acting_trading_sec", "acting_uw_sec", "noncurrent_liab_due_in1y", "other_current_liab",
                     "total_current_liab", "lt_loan", "bond_payable", "lt_payable", "specific_payables",
                     "estimated_liab", "total_dtc", "othr_noncurrent_liab", "total_noncurrent_liab", "total_liab",
                     "shares", "capital_reserve", "treasury_share", "surplus_rese", "general_risk_provision",
                     "undistr_porfit", "forex_differ", "total_parent_corp_equity", "minority_equity",
                     "total_holders_equity", "total_liab_and_holders_equity"]


def xbrl2dtos(data, stock_code, period:PeriodEnum) -> List[BalanceSheetDTO]:
    columns = data["columns"]
    rows = data["rows"]
    dto_list = list()
    for i in range(len(columns)):
        dto = BalanceSheetDTO()
        dto.stock_code:str = stock_code
        dto.stock_id = int(stock_code.split(".")[0])
        dto.period = period
        dto.year = int(del_html_element(data["columns"][i]["title"]))
        for j in range(len(data["rows"])):
            val = del_html_element(rows[j][del_html_element(columns[i]["field"])])
            val = val.replace("-", "") if val and "-" in val else val
            BalanceSheetSchema.set(dto, _value_index_name[j], val)
        dto_list.append(dto)
    return dto_list
